myApp.controller('IntegrationListController', function ($scope, $http, ngDialog) {
    $scope.info.modifiAble = isUserCanOperateByRole('plan_admin', 'pm');
    $scope.getInfo = function () {
        $scope.info.integrationList = [];
        $http.get(beetle_plan_url + '/integration/getIntegrationPlanList?token_id=' + getTokenId()).success(function (data) {
            $scope.info.integrationList = $scope.integrationListFilter(data.data);
            $http.get(ucloud_master_url + '/api/v1/apply_record/?isOnlyIntegration=true&token_id=' + getTokenId()).success(function (data) {
                if (data.success) {
                    angular.forEach($scope.info.integrationList, function (info) {
                        info.agent_ip = null;
                        angular.forEach(data.data, function (apply) {
                            if (info.integration_id == apply.plan_id) {
                                info.agent_ip = apply.agent_ip;
                            }
                        })
                    });
                }
            });
        });
    };

    $scope.integrationListFilter = function (list) {
        if (isUserCanOperateByRole('plan_admin', 'pm', 'ops', 'qa')) {
            return list;
        }
        var l = [];
        var account = getUserAccount();
        angular.forEach(list, function (integration) {
            var members = integration.members;
            angular.forEach(members, function (user) {
                if (account == user.account) {
                    l.push(integration);
                    return;
                }
            })
        });
        return l;
    };

    $scope.getInfo();

    $scope.total = $scope.info.integrationList.length;
    $scope.page = {size: 15, index: 1};

    $scope.init = function (info) {
        window.location.href = '#/menu/integrationInit/' + info.integration_name;
    };
    $scope.reinit = function (info) {
        openDialog(ngDialog, $scope, "确定要重新规划'" + info.integration_name + "'吗?", true, function (val) {
            if (!val) {
                return;
            }
            $http.get(beetle_plan_url + '/integration/reIntegration?integration_id=' + info.integration_id + '&token_id=' + getTokenId()).success(function (data) {
                if (!data.success) {
                    openDialogAlert(ngDialog, $scope, data.msg);
                } else {
                    $scope.init(info);
                }
            });
        })
    };
    $scope.pigeon = function (info) {
        $http.get(beetle_plan_url + '/integration/pigeonholeIntegration?integration_name=' + info.integration_name + '&token_id=' + getTokenId()).success(function (data) {
            $scope.getInfo();
        });
    };

    $scope.link = function (info) {
        window.location.href = "#/menu/newucList/" + integration_agent_list[0];
    };
});